<template>
  <div class="upload-wrapper">
    <div class="upload-list" v-for="(item,index) in uploadList" :Key="index">
      <template>
        <img :src="item.url" />
        <div class="upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-if="item.status === 'finished'">
        <img :src="item.url" />
        <div class="upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :header="upimgheader"
      :data="params"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg','jpeg','png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      type="drag"
      :action="baseU"
      style="display: inline-block;width:58px;"
    >
      <div style="width: 58px;height:58px;line-height: 58px;">
        <Icon type="ios-camera" size="20"></Icon>
      </div>
    </Upload>
    <p class="imgerr"></p>
    <Modal title="查看大图" v-model="visible">
      <img :src="imgName" v-if="visible" style="width: 100%" />
    </Modal>
  </div>
</template>

<script>
export default {
  name: "uploadImg",
  props: {},
  data() {
    return {
      imgName: "",
      visible: false,
      defaultList: [] //默认图片上传
    };
  },
  computed: {},
  methods: {
    // 删除文件
    handleRemove(file) {
      this.uploadList.splice(file, 1); // 数组删除
    },
    // 文件上传成功的钩子
    handleSuccess(res, file) {
      //   this.formValidate.cover = res.data.path;
      //   file.url = res.data.path;
      //   this.uploadList.push(file);
    },
    // 文件格式验证失败的钩子
    handleFormatError(file) {
      this.$Notice.warning({
        title: "格式错误",
        desc: "只能上传jpg,jpeg,png格式,请检查图片格式"
      });
    },
    // 文件超出指定大小的钩子
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "文件大小超出规定",
        desc: "文件 " + file.name + " 太大了, 不能超过2M."
      });
    },
    // 上传之前的钩子
    handleBeforeUpload(file) {
      const check = this.uploadList.length < 1;
      if (!check) {
        this.$Notice.warning({
          title: "警告",
          desc: "只可以上传一张封面图片"
        });
      }
      return check && this.checkImageWH(file, 690, 356);
    },
    checkImageWH(file, width, height) {
      let self = this;
      return new Promise(function(resolve, reject) {
        let filereader = new FileReader();
        filereader.onload = e => {
          let src = e.target.result;
          const image = new Image();
          image.onload = function() {
            if (width && this.width != width) {
              self.$Notice.error({
                title: "请上传宽为" + width + "的图片"
              });
              reject();
            } else if (height && this.height != height) {
              self.$Notice.error({
                title: "请上传高为" + height + "的图片"
              });
              reject();
            } else {
              resolve();
            }
          };
          image.onerror = reject;
          image.src = src;
        };
        filereader.readAsDataURL(file);
      });
    }
  },
  mounted() {},
  watch: {}
};
</script>
